package com.dmxh.service.impl;

import com.dmxh.mapper.TbContentMapper;
import com.dmxh.pojo.TbContent;
import com.dmxh.service.ContentService;
import com.dmxh.vo.ContentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper tbContentMapper;

    @Override
    public Map<String, Object> queryAllContents(Integer pageNum, Integer pageSize, ContentVo contentVo) {
        /*轮播*/
        List<TbContent> tbContents = tbContentMapper.queryByParam(1l, 2);//内容类目ID , 每页条数
        List<TbContent> tbContents1 = tbContentMapper.queryByParam(2l, 3);
        //vo
        List<ContentVo> contentVOS = tbContents.stream().map(content -> {
            ContentVo contentVO = new ContentVo();
            BeanUtils.copyProperties(content, contentVO);
            return contentVO;
        }).collect(Collectors.toList());
        Stream<ContentVo> contentVOS1 = tbContents1.stream().map(content1 -> {
            ContentVo contentVO = new ContentVo();
            BeanUtils.copyProperties(content1, contentVO);
            return contentVO;
        });
        HashMap<String, Object> map = new HashMap<>();
        map.put("content", contentVOS);
        map.put("content1", contentVOS1);
        return map;
    }

    @Override
    public boolean saveContents(ContentVo contentVo) {
        TbContent tbContent = new TbContent();
        BeanUtils.copyProperties(contentVo,tbContent);
        return (tbContentMapper.insertSelective(tbContent) == 1) ? true : false;
    }

    @Override
    public boolean delContents(Long id) {
        return (tbContentMapper.deleteByPrimaryKey(id) == 1) ? true : false;
    }

    @Override
    public boolean updateContents(ContentVo contentVo) {
        TbContent tbContent = new TbContent();
        BeanUtils.copyProperties(contentVo,tbContent);
        return (tbContentMapper.updateByPrimaryKeySelective(tbContent) == 1) ? true : false;
    }


}
